
----- 获取设备大小((游戏设计大小 640*960, 宽为基准
DWinSizeWidth = 640
DWinSizeHeight = 960
DWinSize = CCDirector:sharedDirector():getWinSize()
DSizeFormat = ""
if DWinSize.width ~= DWinSizeWidth then
    DSizeFormat = string.format("x%d",DWinSize.width)
end
-- 资源目录
ResGetPath = string.format("res%s/",DSizeFormat)
-- 获取目录下的文件
GetPathRes = function(file)
    return string.format("%s%s",ResGetPath,file)
end
-- 当前设备大小比例
DeviceScale = DWinSize.width / DWinSizeWidth
-- 获取设备比例数值
gdv = function(value)
    return value * DeviceScale
end
gdvc = function(value)
    return value / DeviceScale
end
gdccp = function(x,y)
    return ccp(x * DeviceScale, y * DeviceScale)
end

GFGetFont = function(v)
    return v * DeviceScale
end

FontSizeMin = 20 * DeviceScale
FontSize = 24 * DeviceScale
FontSize4 = 28 * DeviceScale
FontSize6 = 30 * DeviceScale
FontSize10 = 34 * DeviceScale
FontSizeMax = 50 * DeviceScale

PName   = 0x01 -- 名称
PGemType= 0x02 -- 宝石类型
PGTime  = 0x03 -- 时间
PScene  = 0x04 -- 场景
PStage  = 0x05 -- 每X大关
PStageS = 0x06 -- 每X小关
POpen   = 0x07 -- 开启条件
PScore  = 0x08 -- 过关分数
PComplete= 0x09 --结束类型
PMonster= 0x0a -- 怪物
PRole   = 0x0b -- 角色
PCount  = 0x0c --数量
PScale  = 0x0d --缩放
PType   = 0x0e --类型
PLevel  = 0x0f --等级
PEff    = 0x10 -- 效果

PAtk    = 0x20 --攻击
PDef    = 0x21 --防御
PLife   = 0x22 --生命
PAtksp  = 0x23 --攻速
PDmgType= 0x24 --伤害类型
PTitle  = 0x25 --备注/描述
PModel  = 0x26 --模型
PSkill  = 0x27 --技能

PAtkFPS	= 0x30 --模型攻击时长
PFightFps = 0x31 --模型命中时长
PHurtFps = 0x32 --模型受伤时长

PSkillFPS   = 0x35 --模型技能施放时长
PSkillHFps  = 0x36 --模型技能出现时长

PSkillEffs  = 0x39 --放出系动作
PSound  = 0x40

PModel2 = 0x41

-- 角色
Role_Soldier    = 0x01 --战士
Role_Master     = 0x02 --法师
Role_Archer     = 0x03 --弓箭手
Role_All        = 0x04 --所有

-- 怪物
Mon_YeZhu    = 0x21 --野猪
Mon_Eyes    = 0x22 --狼
Mon_Eagle   = 0x23 --鹰
Mon_WaterE  = 0x24 --水元素
Mon_Sworder = 0x25 --剑士
Mon_Soldier = 0x26 --战士
Mon_Leader  = 0x27 --首领

--技能
Skill_Ssklv1    = 0x01
Skill_Samlv1    = 0x02
Skill_Shplv1    = 0x03

Skill_Asklv1    = 0x04
Skill_Aamlv1    = 0x05
Skill_Ahplv1    = 0x06

Skill_Msklv1    = 0x07
Skill_Mamlv1    = 0x08
Skill_Mhplv1    = 0x09


-- 道具
Item_Bom        = 0x01
Item_BomArce    = 0x02
Item_BomCorss   = 0x03
Item_BomRandom  = 0x04
Item_HpItem     = 0x05
Item_Tili       = 0x06
Item_TimerUp    = 0x07
Item_TimerStop  = 0x08
Item_Wudi       = 0x09
Item_Baozou     = 0x0a
Item_Fuhuo      = 0x0b
Item_Tips       = 0x0c
Item_AutoSl     = 0x0d

Item_SWeapon    = 0x0e
Item_SArmor     = 0x0f
Item_AWeapon    = 0x10
Item_AArmor     = 0x11
Item_MWeapon    = 0x12
Item_MArmor     = 0x13

Item_Crystal99      = 0x15
Item_Crystal250     = 0x16
Item_Crystal999     = 0x17
Item_Crystal2000    = 0x18

Item_HuiliCrystal   = 0x1a


--攻击类型
DamageType_Melee    = 0x01 --近战
DamageType_Remote   = 0x02 --远程


-- 游戏界面
UI_Main     = 0x01
UI_Begin    = 0x02
UI_HLWS_Game= 0x03
UI_SJSK_Game= 0x04
UI_ZJWG_Game= 0x05
UI_WXHL_Game= 0x06
UI_Select_ZJWG  = 0x10
UI_Load     = 0x0a

UI_Role     = 0x07
UI_Shop     = 0x08
UI_Achieve  = 0x09

UI_RoleShow = 0x20


LayerIdx_Bg = 1
LayerIdx_Brick = 4
LayerIdx_Gui    = 5

-- 体力
Tili_Max    = 6 --最高体力值
Tili_Souji  = 1 --限时收集模式消耗
Tili_TiaoZ  = 1 --挑战模式消耗
Tili_Restore= 240 --体力恢复时间/点  4*60秒

-- 宝石类型
Gem_White   = 0x01
Gem_Blue    = 0x03
Gem_Red     = 0x04
Gem_Yellow  = 0x05
Gem_Cyan    = 0x06
Gem_Purple  = 0x07
Gem_Green   = 0x08
GemTypes =
{
    [Gem_White] = 6,
    [Gem_Blue] = 3,
    [Gem_Red] = 0,
    [Gem_Yellow] = 2,
    [Gem_Cyan] = 4,
    [Gem_Purple] = 1,
    [Gem_Green] = 5
}

Gems_Color =
{
    [Gem_White] = ccc3(120,170,240),
    [Gem_Blue] = ccc3(45, 212, 250),
    [Gem_Red] = ccc3(240,40,40),
    [Gem_Yellow] = ccc3(255,226,30),
    [Gem_Cyan] = ccc3(250,250,60),
    [Gem_Purple] = ccc3(240,80,250),
    [Gem_Green] = ccc3(160,240,15)
}


Gem_BomNor  = 0x0a
Gem_BomSz   = 0x0b
Gem_BomArce = 0x0c
Gem_BomRandom= 0x0d

-- 游戏场景
Scene_Flat  = 0x01     --平原
Scene_Forest    = 0x02 --森林
Scene_Seaside   = 0x03 --海边
Scene_Castle    = 0x04 --城堡

-- 游戏结束类型
Complete_TimeEnd    = 0x01 --时间结束
Complete_FillBrick  = 0x02 --填满砖块
Complete_KillsMaster= 0x03 --杀死所有怪物


GameingCombo = 0

-- 场景大小
Gems_Rows   = 8
Gems_Cols   = 8

-- 最低消除数量
Puz_Number  = 3 - 1 

gemSize = 74
gemSizeH = 74
gemAllSize = 74
GridWidth = gemSize
GridHeight = gemSizeH
gemPosy = 185
gemPosx = (DWinSizeWidth-(Gems_Rows*gemSize))/2 + gemSize/2
gemEndPx = gemPosx + gemSize * (Gems_Rows-1)
gemEndPy = gemPosy + gemSize * (Gems_Cols-1) + gemSize/2


-- 游戏模式
GameMode_Puz    = 0x01 --传统交换三消
GameMode_Bizz   = 0x02 --爆破小鸟
GameMode_Clicked= 0x03 --点击连接消除

PlayMode_ShouJi = 0x01
PlayMode_TiaoZhan= 0x02

GameMode_HLWS   = 0x01
GameMode_SJSK   = 0x02
GameMode_ZJWG   = 0x03
GameMode_WXHL   = 0x04


--暴走时长(秒
GBaoZouTimer    = 10
GPlayGameTime   = 91 --游戏时长(局/秒
GItmeP_TimeUp   = 20 --增加游戏时间(道具
GItemP_TimeStop = 10 --暂停
GItemP_Wudi     = 5 --无敌时长


GHLWSComboFps = 1.3
GComboFps = 1.4 --连击频率
GPuzComboFps = 1.0 --连击频率

-------------动画参数
Anim_FadeOut    = 0.9 -- 爆炸消失时间
Anim_MoveTimes  = 0.12 -- 移动时长/每宝石距离
Anim_SwapTimes  = Anim_MoveTimes*2 -- 交换时长

Action_MoveTo   = 0x01
Action_CCFadeOut= 0x02
Action_JumpTo   = 0x03

--------------------- 通用函数
-- 获取应该放置的准确坐标
function getGemPos(r, c)
    return gdv(gemPosx + GridWidth*c), gdv(DWinSizeHeight - (gemPosy + GridHeight*r))
end
function getGemPosNoSc(r, c)
    return gemPosx + GridWidth*c, DWinSizeHeight - (gemPosy + GridHeight*r)
end
function getGemGrid(x, y)
    local c = (x - gemPosx + GridWidth/2) / GridWidth
    local r = (DWinSizeHeight - gemPosy - y + GridHeight/2) / GridHeight
    --print(math.floor(r),math.floor(c))
    return math.floor(r),math.floor(c)
end
-- 通过行/列,获得key
function getIndex(row, col)
    return col * Gems_Rows + row
end




StrTili = "tili"
StrSoulPower = "soulpower"
function Func_StrStage(stage)
    return table.concat({"hlws",stage,"_score"})
end
function Func_StrCombo(stage)
    return table.concat({"hlws",stage,"_combo"})
end
--关卡分数
-- CCUserDefault:sharedUserDefault():
--写入数据,setBoolForKey,setIntegerForKey,setFloatForKey,setDoubleForKey,setStringForKey
--参数(字符串名称,数据)
--获取为相应的get,  更新数据为 flush

--魂力无双
function UpdateStageDatas(stage, dtable)
    CCUserDefault:sharedUserDefault():setIntegerForKey(Func_StrStage(stage), dtable)
    CCUserDefault:sharedUserDefault():flush()
end
function GetStageDatas(stage)
    local ld_score = CCUserDefault:sharedUserDefault():getIntegerForKey(Func_StrStage(stage))
    --local ld_soul = CCUserDefault:sharedUserDefault():getIntegerForKey(StrSoulPower)
    --local ld_combo = CCUserDefault:sharedUserDefault():getIntegerForKey(Func_StrCombo(stage))
    return ld_score
end
--水晶时空
function UpdateSJSKDatas(dtable)
    CCUserDefault:sharedUserDefault():setIntegerForKey("sjsk_score", dtable)
    CCUserDefault:sharedUserDefault():flush()
end
function GetSJSKDatas()
    local ld_score = CCUserDefault:sharedUserDefault():getIntegerForKey("sjsk_score")
    return ld_score
end
--拯救王国
function UpdateZJWGatas(stage, dtable)
    CCUserDefault:sharedUserDefault():setIntegerForKey(table.concat({"zjwg",stage,"_score"}), dtable)
    CCUserDefault:sharedUserDefault():flush()
end
function GetZJWGDatas(stage)
    local ld_score = CCUserDefault:sharedUserDefault():getIntegerForKey(table.concat({"zjwg",stage,"_score"}))
    return ld_score
end
function GetMaxZJWGScore()
    return CCUserDefault:sharedUserDefault():getIntegerForKey("zjwgMax")
end
function SetMaxZJWGScore(v)
    CCUserDefault:sharedUserDefault():setIntegerForKey("zjwgMax", v)
    CCUserDefault:sharedUserDefault():flush()
end
function GetMaxWXHLScore()
    return CCUserDefault:sharedUserDefault():getIntegerForKey("wxhlMax")
end
function SetMaxWXHLScore(v)
    CCUserDefault:sharedUserDefault():setIntegerForKey("wxhlMax", v)
    CCUserDefault:sharedUserDefault():flush()
end
--水晶币
function GetCrystal(v)
    return CCUserDefault:sharedUserDefault():getIntegerForKey("crystal")
end
function AddCrystal(v)
    if v == nil then
        v = 0
    end
    
    if v > 0 then
        Func_DataStatis(DS_Crystal, v)
    end
    
    CCUserDefault:sharedUserDefault():setIntegerForKey("crystal", GetCrystal() + v)
    CCUserDefault:sharedUserDefault():flush()
end
--魂力值
function GetSoulPower()
    local ld_soul = CCUserDefault:sharedUserDefault():getIntegerForKey(StrSoulPower)
    return ld_soul
end
function AddSoulPower(ld_soul)
    local lastSoulPower = GetSoulPower()
    
    CCUserDefault:sharedUserDefault():setIntegerForKey(StrSoulPower, GetSoulPower() + ld_soul)
    CCUserDefault:sharedUserDefault():flush()
    
    
    --- 赠送物品奖励**魂力为1000的倍数(递增
    --local score = 1000
    --while true do
    --    if lastSoulPower < score then
    --        if GetSoulPower() >= score then
    --            -- 获得
    --            local msg = string.format("获得魂力值达到 %d", score)
    --            GFunc_ZhenSong(msg)
    --        else
    --            break
    --        end
    --    else
    --        break
    --    end
    --    score = score * 2
    --end
end
--体力值
function GetTili()
    return CCUserDefault:sharedUserDefault():getIntegerForKey(StrTili)
end
function AddTili(v)
    CCUserDefault:sharedUserDefault():setIntegerForKey(StrTili, GetTili() + v)
    CCUserDefault:sharedUserDefault():flush()
end
function RedTili()
    CCUserDefault:sharedUserDefault():setIntegerForKey(StrTili, GetTili() - 1)
    CCUserDefault:sharedUserDefault():flush()
    Func_DataStatis(DS_Tili)
end
--技能(名字=通过技能ID返回相应的技能等级)
function GetSkill(skid)
    return CCUserDefault:sharedUserDefault():getIntegerForKey(string.format("skill_%d",skid))
end
function UpSkill(skid, lv)
    CCUserDefault:sharedUserDefault():setIntegerForKey(string.format("skill_%d",skid), lv)
    CCUserDefault:sharedUserDefault():flush()
end

--道具
function GetItems(item)
    return CCUserDefault:sharedUserDefault():getIntegerForKey(string.format("item_%d",item))
end
function UpItems(item, cut)
    CCUserDefault:sharedUserDefault():setIntegerForKey(string.format("item_%d",item), GetItems(item)+cut)
    
    CCUserDefault:sharedUserDefault():flush()
end

--音乐音效开关
function SetSoundOff(v)
    CCUserDefault:sharedUserDefault():setIntegerForKey("sound", v)
    CCUserDefault:sharedUserDefault():flush()
end
function GetSoundOff()
    return CCUserDefault:sharedUserDefault():getIntegerForKey("sound")
end
---
function SetFirstTutorial(v)
    CCUserDefault:sharedUserDefault():setIntegerForKey(string.format("firstTut%d",v), 1)
    CCUserDefault:sharedUserDefault():flush()
end
function GetFirstTutorial(v)
    return CCUserDefault:sharedUserDefault():getIntegerForKey(string.format("firstTut%d",v))
end

function UseItems(item)
    if GetItems(item) > 0 then
        UpItems(item, -1)
        --Play_Effect(sme_Click)
        --播放音效
        if ShopItems_Infos[item][PSound] ~= nil then
            Play_Effect(ShopItems_Infos[item][PSound])
        else
            Play_Effect(sme_Click)
        end
        
        return true
    end
    
    Play_Effect(sme_ClickError)
    return false
end

--成就
function GetAchieves(achid)
    local sname = string.format("ach_%s",achid)
    return CCUserDefault:sharedUserDefault():getIntegerForKey(sname)
end
function SetAchieves(achid, cut)
    local sname = string.format("ach_%s",achid)
    CCUserDefault:sharedUserDefault():setIntegerForKey(sname, cut)
    CCUserDefault:sharedUserDefault():flush()
end

--获得玩家头衔
function GetMyTitles()
    local mytitles = ""
    
    local value = GetAchieves(DS_Skill)
    if value >= 5120 then
        mytitles = "无双战神"
    elseif value >= 2560 then
        mytitles = "地狱恶魔"
    elseif value >= 1280 then
        mytitles = "王国复仇者"
    elseif value >= 640 then
        mytitles = "银翼杀手"
    elseif value >= 320 then
        mytitles = "战斗英雄"
    elseif value >= 160 then
        mytitles = "铁血英豪"
    elseif value >= 80 then
        mytitles = "精英勇士"
    elseif value >= 40 then
        mytitles = "一等兵"
    elseif value >= 20 then
        mytitles = "二等兵"
    else
        local achName = table.concat({DS_ModeOp,"_", GameMode_ZJWG})
        if GetAchieves(achName) > 0 then
            mytitles = "三等兵"
        end
    end
    
    return mytitles
end






